#include "system.h"


typedef struct flapp_net
{
	uint32_t filter_set_time;
	uint32_t filter_work_time;
}flapp_net_t;

struct flapp_net cur_net_buf;

/**********************************************************
函数功能：上电初始化滤网
入口参数：无
出口参数：无
说	  明 
***********************************************************/ 
void flapp_save_init(void)
{
	uint16_t filter_set_time[2] = {0};
	uint32_t filter_read_time;
	fldrv_read_word_flash(FILTER_SET_TIME_ADDR,&filter_read_time);			//获取设置滤网时长，如果没有设置默认2000小时
	if(filter_read_time >> 24 != FLASH_SAVE_HEAD)
	{
		fldrv_flash_write(FILTER_RUN_TIME_ADDR,filter_set_time,2);
		cur_net_buf.filter_work_time = FALSE;																									//首次开机工作时长0分钟
		filter_set_time[1] = ((FLASH_SAVE_HEAD << 8) + (FILTER_DEFAULT_TIME >> 16));
		filter_set_time[0] = (uint16_t)FILTER_DEFAULT_TIME;
		fldrv_flash_write(FILTER_SET_TIME_ADDR,filter_set_time,2);
		cur_net_buf.filter_set_time = ((filter_set_time[1] << 16) + filter_set_time[0]) & 0xffffff;				//更新滤网设置时长
	}
	else
	{
		cur_net_buf.filter_set_time = filter_read_time & 0xffffff;					//掉电开机更新最后一次设置滤网时长
		fldrv_read_word_flash(FILTER_RUN_TIME_ADDR,&filter_read_time);			//掉电开机更新最后一次滤网工作时长
		cur_net_buf.filter_work_time = filter_read_time & 0xffffff;
	}
}

/**********************************************************
函数功能：网络模块返回函数
入口参数：uint8_t* send_buf,uint8_t* remote_buf
出口参数：无
说	  明 
***********************************************************/ 
void fldrv_uart_send_back(uint8_t* send_buf,uint8_t* remote_buf)
{
	uint8_t i;
	uint8_t send_back_buf[NET_WR_ENUM_END] = {0};
	struct flapp_dis get_dis_content;
//	static uint8_t lamp_mark = FALSE;	//结合滤网设置上报到平台
//	static uint16_t last_slave_err = FALSE;	//变化了就将灯光标志切换
	get_dis_content = flapp_get_device_status();
	for(i = 0;i < NET_WR_ENUM_END;i++)
	{
		send_back_buf[i] = send_buf[i];
	}
//	if(get_dis_content.slave_err_report != last_slave_err)
//	{
//		lamp_mark ^= TRUE;
//		last_slave_err = get_dis_content.slave_err_report;
//	}
	send_back_buf[NET_WR_HEAD]		= 0xAA;
	send_back_buf[NET_WR_SWITCH_VAL] = 0x80 + get_dis_content.flag_filter * BIT2 + get_dis_content.flag_ion * BIT4 + \
																			GET_BIT(remote_buf[NET_WR_SWITCH_VAL],WR_REMOTE_CON) * BIT1;// + lamp_mark * BIT3;
	send_back_buf[NET_WR_FAN_SPEED]  = get_dis_content.air_level;
	send_back_buf[NET_WR_WORK_MODE]  = get_dis_content.flag_auto;
	send_back_buf[NET_WR_LIFTTIME_H] = (cur_net_buf.filter_work_time / 60) >> 8;
	send_back_buf[NET_WR_LIFTTIME_L] = cur_net_buf.filter_work_time / 60;
	send_back_buf[NET_WR_PM_LEVEL]	 = get_dis_content.air_level;
	send_back_buf[NET_WR_VOC_LEVEL]	 = get_dis_content.air_level;
	send_back_buf[NET_WR_PM_H]			 = get_dis_content.value_air[0] >> 8;
	send_back_buf[NET_WR_PM_L]			 = get_dis_content.value_air[0];
	send_back_buf[NET_WR_VOC_H]			 = get_dis_content.value_air[1] >> 8;
	send_back_buf[NET_WR_VOC_L]			 = get_dis_content.value_air[1];
	send_back_buf[NET_WR_TEMP]			 = get_dis_content.val_t_h_buf[0];
	send_back_buf[NET_WR_HUM]				 = get_dis_content.val_t_h_buf[1];
//	send_back_buf[NET_WR_CADR_H]		 = get_dis_content.slave_err_report >> 8;
//	send_back_buf[NET_WR_CADR_L]		 = get_dis_content.slave_err_report;
	send_back_buf[NET_WR_ERROR]			 = get_dis_content.master_err;
	send_back_buf[NET_WR_DEVICE_TYPE]= DEVICE_TYPE;
	send_back_buf[NET_WR_SOFT_VERSION] = SYS_VERSION;
//	send_back_buf[NET_WR_SET_LIFTTIME_H] = (cur_net_buf.filter_set_time / 60) >> 8;
//	send_back_buf[NET_WR_SET_LIFTTIME_L] = cur_net_buf.filter_set_time / 60;
	send_back_buf[NET_WR_SLAVE_ERROR_H] = get_dis_content.slave_err_report >> 8;	//设置滤网时长代替从机故障上报
	send_back_buf[NET_WR_SLAVE_ERROR_L] = get_dis_content.slave_err_report;
	send_back_buf[NET_WR_CHECKNUM]	 = fldrv_check_sum(send_back_buf,NET_WR_ENUM_END - 1);
	fldrv_uart_send_buf(USART1,send_back_buf,sizeof(send_back_buf));
	
	if(remote_buf[REMOTE_VAILD] || get_dis_content.flag_net == FALSE)	//当有远程下发或者网络连接成功
	{
			SET_BIT(remote_buf[REMOTE_VAILD],VAILD_SWITCH_NET);
			xQueueSend(remote_con_queue,remote_buf,0);
	}
}

/**********************************************************
函数功能：网络远程控制处理
入口参数：net_rcv_buf
出口参数：无
说	  明 
***********************************************************/ 
void flapp_update_display_backand(uint8_t* net_rcv_buf)
{
	uint8_t remote_queue[REMOTE_NUM] = {0};				//远程控制到显示
	uint8_t net_send_buf[NET_WR_ENUM_END] = {0};	//net返回
	if(net_rcv_buf[NET_RD_COMMAND] == CON_CMD)			//远程控制指令
	{
		SET_BIT(net_send_buf[NET_WR_SWITCH_VAL],WR_REMOTE_CON);
		if(GET_BIT(net_rcv_buf[NET_RD_DATA_VALID],DATA_VAILD_MODE))		//远程控制模式
		{
			SET_BIT(remote_queue[REMOTE_VAILD],VAILD_SWITCH_AUTO);
			remote_queue[REMOTE_AUTO] = net_rcv_buf[NET_RD_WORK_MODE] & 0xf1;	//模式只有手动和自动，其他设置不生效
			net_send_buf[NET_WR_WORK_MODE] = net_rcv_buf[NET_RD_WORK_MODE] & 0xf1;
		}
		
		if(GET_BIT(net_rcv_buf[NET_RD_SWITCH_VALID],SWITCH_VAILD_ION))		//远程控制离子
		{
			SET_BIT(remote_queue[REMOTE_VAILD],VAILD_SWITCH_ION);
			SET_BIT(net_send_buf[NET_WR_SWITCH_VAL],WR_ION * GET_BIT(net_rcv_buf[NET_RD_SWITCH_VAL],SWITCH_VAILD_ION));
			remote_queue[REMOTE_ION] = GET_BIT(net_rcv_buf[NET_RD_SWITCH_VAL],SWITCH_VAILD_ION);
		}
		
		if(GET_BIT(net_rcv_buf[NET_RD_SWITCH_VALID],SWITCH_VAILD_FIL_RST))	//远程控制滤网重置
		{
			uint16_t filter_set_time[2] = {0};
			SET_BIT(remote_queue[REMOTE_VAILD],VAILD_SWITCH_FIL_RST);
			SET_BIT(net_send_buf[NET_WR_SWITCH_VAL],WR_FIL_RST * GET_BIT(net_rcv_buf[NET_RD_SWITCH_VAL],SWITCH_VAILD_FIL_RST));
			remote_queue[REMOTE_FILTER_RESET] = FALSE;
			cur_net_buf.filter_work_time = FALSE;
			filter_set_time[0] = cur_net_buf.filter_work_time;
			filter_set_time[1] = (FLASH_SAVE_HEAD << 8) + ((cur_net_buf.filter_work_time & 0xff0000) >> 16);
			fldrv_flash_write(FILTER_RUN_TIME_ADDR,filter_set_time,2);
		}
		
		if(GET_BIT(net_rcv_buf[NET_RD_DATA_VALID],DATA_VAILD_FAN))				//远程控制风速
		{
			SET_BIT(remote_queue[REMOTE_VAILD],VAILD_SWITCH_FAN);
			remote_queue[REMOTE_FAN_SPEED] = net_rcv_buf[NET_RD_FAN_SPEED];
			net_send_buf[NET_WR_FAN_SPEED] = net_rcv_buf[NET_RD_FAN_SPEED];
		}
		
		if(GET_BIT(net_rcv_buf[NET_RD_DATA_VALID],DATA_VAILD_FIL_TIME))			//远程设置滤网时长
		{
			uint32_t tmp;
			uint16_t filter_set_time[2] = {0};
			tmp = ((net_rcv_buf[NET_RD_LIFTTIME_H] << 8) + net_rcv_buf[NET_RD_LIFTTIME_L]) * 60;
			filter_set_time[1] = ((FLASH_SAVE_HEAD << 8) + (tmp >> 16));
			filter_set_time[0] = tmp;
			fldrv_flash_write(FILTER_SET_TIME_ADDR,filter_set_time,2);			//写flash滤网时长
			cur_net_buf.filter_set_time = tmp;															//更新滤网使用时长
			net_send_buf[NET_WR_SET_LIFTTIME_H] = net_rcv_buf[NET_RD_LIFTTIME_H];
			net_send_buf[NET_WR_SET_LIFTTIME_L] = net_rcv_buf[NET_RD_LIFTTIME_L];
		}
	}
	net_send_buf[NET_WR_FRAME_NUM] = net_rcv_buf[NET_RD_FRAME_NUM];
	fldrv_uart_send_back(net_send_buf,remote_queue);
}

/**********************************************************
函数功能：网络任务运行函数
入口参数：无
出口参数：无
说	  明 
***********************************************************/ 
void flapp_net_run(void)
{
	static uint8_t net_disconnect_count = NET_DISCONNECT_CNT;	//4G模块断开计数，默认断开连接
	static uint16_t fil_100ms_count = 0;
	uint8_t net_rcv_buf[NET_RD_ENUM_END];
	uint8_t net_queue[REMOTE_NUM] = {0};
	
	if(flapp_get_device_type() != DEVICE_MASTER)	//只有作为主机才有网络部分通讯功能
		return;
	
	fil_100ms_count++;
	if(fil_100ms_count >= SYS_TIME_10MIN || fil_100ms_count == SYS_TIME_1S)		//10分钟更新一次滤网运行时长，再就是上电初始化完1秒后
	{
		uint16_t filter_set_time[2] = {0};
		if(fil_100ms_count >= SYS_TIME_10MIN)	//首次上电不更新数据，只做状态显示
		{
			cur_net_buf.filter_work_time += 10;	//10分钟
			filter_set_time[0] = cur_net_buf.filter_work_time;
			filter_set_time[1] = (FLASH_SAVE_HEAD << 8) + ((cur_net_buf.filter_work_time & 0xff0000) >> 16);
			fldrv_flash_write(FILTER_RUN_TIME_ADDR,filter_set_time,2);
		}
		if(cur_net_buf.filter_work_time >= cur_net_buf.filter_set_time)
		{
			SET_BIT(net_queue[REMOTE_VAILD],VAILD_SWITCH_FIL_TMR);				//滤网使用寿命到，设置到显示端
		}
		fil_100ms_count = SYS_TIME_1S;
	}
	if(xQueueReceive(net_rd_queue,net_rcv_buf,0) != pdTRUE)					//没有接收到消息
	{
		net_disconnect_count++;
		
		if((net_disconnect_count >= NET_DISCONNECT_CNT) || net_queue[REMOTE_VAILD])
		{
			if(net_disconnect_count < NET_DISCONNECT_CNT)					//4G刚断开的时候还是正常显示
			{
				SET_BIT(net_queue[REMOTE_VAILD],VAILD_SWITCH_NET);
			}
			else if(net_disconnect_count >= NET_DISCONNECT_CNT)
			{
				if(net_disconnect_count == NET_DISCONNECT_CNT)
				{
					xQueueSend(remote_con_queue,net_queue,0);
				}
				net_disconnect_count = NET_DISCONNECT_CNT + 1;
			}
			if(net_queue[REMOTE_VAILD])		//有效位不为0的时候才发送
			{
				xQueueSend(remote_con_queue,net_queue,0);	
			}				
		}
		return;
	}
	
	net_disconnect_count = FALSE;
	flapp_update_display_backand(net_rcv_buf);	//数据转换后发送到显示任务
}
